
/**
 * created on 2012-05-20
 * @author  qiujz
 * @version 1.0
 *
 * Copyright (c)  Founder Software (Suzhou) Co., Ltd.  2012
 *  All rights reserved.
 *
 * 
 * ================================================================
 * Change Revision
 * ---------------------------------------------------------------
 *    DATE                BY                   COMMENT
 * ---------------------------------------------------------------
 * 2012-05-20         qiujz                Create class
 * 
 *
 * ================================================================
 */

package com.qjz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.founder.mh.framework.entity.QueryResultBase;
import com.founder.mh.framework.util.BuildTableUtil;
import com.qjz.dao.IFunctionOpenDao;
import com.qjz.entity.FunctionOpenQueryParameter;
import com.qjz.entity.FunctionOpenTo;
import com.qjz.service.IFunctionOpenService;
import com.founder.mh.framework.entity.Sort;
import com.founder.mh.framework.service.impl.BaseServiceImpl;

@WebService(endpointInterface = "com.qjz.service.IFunctionOpenService")
public class FunctionOpenServiceImpl extends BaseServiceImpl implements IFunctionOpenService {

	private static final Log LOGGER = LogFactory.getLog(FunctionOpenServiceImpl.class);

	private IFunctionOpenDao functionOpenDao;
	
	/**
	 * @return Dao接口
	 */
	public IFunctionOpenDao getFunctionOpenDao() {
		return functionOpenDao;
	}
	
	/**
	 * @param functionOpenDao
	 *            Dao接口
	 */
	public void setFunctionOpenDao(IFunctionOpenDao functionOpenDao) {
		this.functionOpenDao = functionOpenDao;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #searchFunctionOpenList(com.qjz.entity.FunctionOpenQueryParameter)
	 */
	public QueryResultBase searchFunctionOpenList(FunctionOpenQueryParameter paramter) {
		if (paramter == null) {
			throw new RuntimeException("查询参数对象不能为空!");
		}
		
		// 放置查询结果的对象
		QueryResultBase rtnResult = new QueryResultBase();

		// 设置查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// ToDO Add parameter
		FunctionOpenTo functionOpen=paramter.getFunctionOpen();
		if(functionOpen != null){
			if(functionOpen.getFunType() != 0)
				paramMap.put(BuildTableUtil
							.buildPropertyNameFromColumn(FunctionOpenTo.FUN_TYPE), functionOpen.getFunType());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(FunctionOpenTo.START_DATE), functionOpen.getStartDate());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(FunctionOpenTo.END_DATE), functionOpen.getEndDate());
		}

		List<FunctionOpenTo> functionOpenList = null;
		int total = functionOpenDao.getFunctionOpenCount(paramMap);
		rtnResult.setTotal(total);
		if (total == 0) {
			return rtnResult;
		}
		
		// 设置排序字段
		List<Sort> sortList = paramter.getSort();
		if (sortList != null && sortList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Sort sort : sortList) {
				sb.append(sort.getColumnName()).append(" ").append(
						sort.getOrder()).append(",");
			}

			paramMap.put("sort", sb.toString()
					.substring(0, sb.lastIndexOf(",")));

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(paramMap.get("sort"));
			}
		}
		
		// 不分页的情况
		if (paramter.getStartRow() <= 0) {
			functionOpenList= functionOpenDao.searchFunctionOpenList(paramMap);
		} else {
			// 执行分页询
			int endRow = paramter.getStartRow() + paramter.getLimit() - 1;
			functionOpenList = functionOpenDao.searchFunctionOpenPagination(paramMap,
					paramter.getStartRow(), endRow);
		}
		rtnResult.setPageData(functionOpenList);
		return rtnResult;
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #getFunctionOpenByPrimaryKey(int)
	 */
	public FunctionOpenTo getFunctionOpenByPrimaryKey(int foid) {
		return functionOpenDao.getFunctionOpenByPrimaryKey(foid);
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #insertFunctionOpen(com.qjz.entityFunctionOpen.FunctionOpenTo)
	 */
	public FunctionOpenTo insertFunctionOpen(FunctionOpenTo functionOpen) {
		
			 functionOpenDao.insertFunctionOpen(functionOpen);
			 //functionOpen = functionOpenDao.getFunctionOpenByPrimaryKey(foid);
		
		
		return functionOpen;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #updateFunctionOpen(com.qjz.entity.FunctionOpenTo)
	 */
	public int updateFunctionOpen(FunctionOpenTo functionOpen) {
		return functionOpenDao.updateFunctionOpenByBean(functionOpen);		
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #deleteFunctionOpen(int)
	 */
	public int deleteFunctionOpen(int foid) {
		return functionOpenDao.deleteFunctionOpenByPrimaryKey(foid);
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IFunctionOpenService
	 *      #deleteFunctionOpenBatch(int[])
	 */
	public void deleteFunctionOpenBatch(int[] keyArray) {
		functionOpenDao.deleteFunctionOpenBatch(keyArray);
	}
	
	public void submitFuncOpen(List<FunctionOpenTo> list){
		functionOpenDao.deleteFunctionOpen();
		for(FunctionOpenTo fo : list){
			functionOpenDao.insertFunctionOpen(fo);
		}
	}
	
}
	
